package com.tanhua.server.service;

import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.RecommendVoice;
import com.tanhua.domain.mongo.Voice;
import com.tanhua.domain.vo.ChuanyinVo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.PeachblossomApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PeachblossomService {
    @Reference
    private PeachblossomApi peachblossomApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 接收语音
     * @return
     */
    public ChuanyinVo receiveVoice() {

        Long userId = UserHolder.getUserId();
        String key="Uservoice_"+userId;
        //将剩余次数存入redis中
        Integer count = (Integer)redisTemplate.opsForValue().get(key);

        //如果剩余次数为空则是第一次
        if (count==null){
            redisTemplate.opsForValue().set(key,2);
            count=2;
        }else if (count>0){
            count=count -1;
            redisTemplate.opsForValue().set(key,count);

        }else {
            throw  new TanHuaException("次数不足");
        }

        UserInfo userInfoById = userInfoApi.findUserInfoById(userId);
        RecommendVoice recommendUser=new RecommendVoice();
        UserInfo userInfo=new UserInfo();
        Voice voice=new Voice();
        String gender="woman";
        String loginUserGender = userInfoById.getGender();
        //只推荐异性
        if (loginUserGender.equals("man")){
            //查询语音推荐表查询分数最高用户信息
            recommendUser =peachblossomApi.findRecommendUser(userId,gender);
            long voiceid = recommendUser.getVoiceId();
            userInfo = userInfoApi.findUserInfoById(voiceid);

            voice=peachblossomApi.findSendUSer(voiceid);
            peachblossomApi.deleteRecommendUser(voiceid);
        }else {
            //查询语音推荐表查询分数最高用户信息
            gender="man";
            recommendUser =peachblossomApi.findRecommendUser(userId,gender);
            long voiceid = recommendUser.getVoiceId();
            userInfo = userInfoApi.findUserInfoById(voiceid);
            voice=peachblossomApi.findSendUSer(voiceid);
            peachblossomApi.deleteRecommendUser(voiceid);
        }


        //封装返回
        ChuanyinVo chuanyinVo=new ChuanyinVo();
        BeanUtils.copyProperties(userInfo,chuanyinVo);
        chuanyinVo.setSoundUrl(voice.getSoundUrl());
        chuanyinVo.setId(voice.getUserId().intValue());
        chuanyinVo.setRemainingTimes(count);
        log.debug("count:"+count);
        return chuanyinVo;
    }
}
